package com.itheima.web.servlet;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.service.ServiceIml;
import com.itheima.service.UserService;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {

    //分页查询
    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 获取分页参数
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        //获取查询条件
        Map<String, String[]> map = request.getParameterMap();
        User queryUser=new User();
        BeanUtils.populate(queryUser,map);

        //调用service查询,返回PageInfo对象
        UserService service = new ServiceIml();
        PageInfo<User> page = service.pageQuery(currentPage, pageSize,queryUser);
        request.setAttribute("queryUser",queryUser);
        request.setAttribute("page", page);

        request.getRequestDispatcher("/list.jsp").forward(request, response);


    }
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        //获取参数id
        String id = request.getParameter("id");
        //调用service删除
        UserService service=new ServiceIml();
        service.deleteUser(id);
        //重定向到userListServlet
        try {
            response.sendRedirect(request.getContextPath()+"/userServlet?method=pageQuery");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void add(HttpServletRequest request, HttpServletResponse response) {
        try {
            //设置编码
            request.setCharacterEncoding("utf-8");
            //获取所有数据
            Map<String, String[]> parameterMap = request.getParameterMap();

            //封装对象
            User user = new User();
            BeanUtils.populate(user,parameterMap);

            //调用service中方法
            UserService service=new ServiceIml();
            service.addUser(user);


            //跳转到userListServlet，再次查询
            response.sendRedirect(request.getContextPath()+"/userServlet?method=pageQuery");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void findUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户id
        String id = request.getParameter("id");
        //调用Service查询用户信息
        UserService service=new ServiceIml();
        User user= service.findUserById(id);
        //将user对象封装request域中
        request.setAttribute("user",user);
        //转发到update.jsp页面
        request.getRequestDispatcher("update.jsp").forward(request,response);
    }
    public void updateUser(HttpServletRequest request, HttpServletResponse response) {
        try {

            //获取表单数据
            Map<String, String[]> parameterMap = request.getParameterMap();
            //封装到User对象中
            User user = new User();
            BeanUtils.populate(user,parameterMap);
            //调用Service中的方法
            UserService service=new ServiceIml();
            service.updateUser(user);
            //重定向到userListServlet
            response.sendRedirect(request.getContextPath()+"/userServlet?method=pageQuery");
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service查询
        UserService service = new ServiceIml();
        List<User> users= service.findAll();
        //存入到request域中
        request.setAttribute("users",users);
        //转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
        }
    public void delSelected(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有id
        String[] ids = request.getParameterValues("uid");

        //调用Service中的方法
        UserService service=new ServiceIml();
        service.delSelectedUser(ids);

        //跳转到userListServlet
        response.sendRedirect(request.getContextPath()+"/userServlet?method=pageQuery");
    }

}
